package com.zd.wechatPlatform.bean.model;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.zd.wechatPlatform.common.annotation.Column;
import com.zd.wechatPlatform.common.utils.StringUtils;

public abstract class BaseModel {
	abstract Integer getId();
	abstract void setId(Integer id);
	abstract Date getCreateTime();
	abstract void setCreateTime(Date createTime);
	abstract Date getUpdateTime();
	abstract void setUpdateTime(Date updateTime);
	
	public Map<String, String> getInsertMap() {
		Map<String, String> insertMap=new HashMap<String, String>();
		Class<?> modelClass = this.getClass();
		String classNames="t"+modelClass.getName().substring(modelClass.getName().lastIndexOf(".")+1);
		String className=StringUtils.toUnderlineName(classNames);
		Method[] methods = modelClass.getMethods();	
		StringBuilder columnInsert=new StringBuilder();
		StringBuilder columnReturn=new StringBuilder();
		StringBuilder columnUpdate=new StringBuilder();
		for (int i=0;i<methods.length;i++) {
			if(methods[i].isAnnotationPresent(Column.class)){
				String column=methods[i].getName();
					String columns=column.substring(3).substring(0,1).toLowerCase()+column.substring(4);
					String columnInse=StringUtils.toUnderlineName(columns);
					columnUpdate.append(columnInse+"=#{"+columns+"}");
					columnReturn.append("#{"+columns+"}");
					columnInsert.append(columnInse);
					columnUpdate.append(",");
					columnInsert.append(",");
					columnReturn.append(",");
			}
		}
		String columnInserts=columnInsert.toString().substring(0, columnInsert.length()-1);
		String columnReturns=columnReturn.toString().substring(0, columnReturn.length()-1);
		String columnUpdates=columnUpdate.toString().substring(0, columnUpdate.length()-1);
		insertMap.put("className", className);
		insertMap.put("columnInsert",columnInserts );
		insertMap.put("columnReturn",columnReturns );
		insertMap.put("columnUpdate",columnUpdates );
		return insertMap;	
	}
}
